package com.fmdj.dr.service.impl;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.fmdj.dr.db.dao.DriverSettingsDao;
import com.fmdj.dr.db.pojo.DriverSettingsEntity;
import com.fmdj.dr.service.DriverSettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;


@Service
@Slf4j
public class DriverSettingsServiceImpl implements DriverSettingsService {

    @Autowired
    private DriverSettingsDao driverSettingsDao;
    @Override
    public int insertDriversSettings(DriverSettingsEntity entity) {
        return driverSettingsDao.insertDriversSettings(entity);
    }

    @Override
    public HashMap selectDriverSettings(Long driverId) {
        //取出查询出的数据(json字符串)
        String settings = driverSettingsDao.selectDriverSettings(driverId);

        //将json字符串(本质是json对象)转化成json对象再转为map
        HashMap resultMap =  JSONUtil.parseObj(settings).toBean(HashMap.class);

        //再将resultMap中的{"autoAccept": false,  "listenService": true} 转化成 布尔型 因为虽然在数据库中是保存的布尔型数据，但是取出来是整形 0或者1 (前端需要布尔型)所以这里需要转化成布尔型
        Boolean autoAccept = MapUtil.getInt(resultMap, "autoAccept") == 1 ? true : false;
        Boolean listenService = MapUtil.getInt(resultMap, "listenService") == 1 ? true : false;

        //将转化后的数据替换掉
        resultMap.replace("autoAccept", autoAccept);
        resultMap.replace("listenService", listenService);

        return resultMap;
    }
}
